home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6862 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  82 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strange segementtation fault w/ fprintf
  5. Date: 15 Feb 1996 20:59:54 GMT
  6. Organization: OpenVision
  7. Message-ID: <4g06sa$o20@spanky.pls.ov.com>
  8. References: <312215BE.41C6@di.epfl.ch>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 41C6@di.epfl.ch, Olivier Georg <ogeorg@di.epfl.ch> writes:
  13. >Hi, 
  14. >
  15. >I have a very strange run-time error.  I'm using fprintf
  16. >to write to a file, but I get a core dumped while everything
  17. >seems OK.  Here's the code:
  18. >
  19. >  main() {
  20. >    ...
  21. >    printf("variable to save data to:"); scanf("%s", &varname);
  22. >    strcpy(filename, varname); strcat(filename, ".yyy");
  23. >
  24. >at this point filename has value, say, "b.yyy"
  25. >
  26. >    fp = fopen(filename,"w");
  27. >
  28. >fp has value 0xfb52924, so it is not 0...
  29. >
  30. >    writevector(fp, totalLength, toSave);
  31. >    fclose(fp);
  32. >    ...
  33. >  }
  34. >
  35. >writevector is:
  36. >
  37. >  int writevector(FILE *fp,
  38. >          int len,
  39. >          double vect[])
  40. >  {
  41. >    char str[64];
  42. >    int i = 0;
  43. >    for (i=0; i<len; i++) {
  44. >      printf("vect[%d] = %f\n", i, vect[i]);
  45. >      fprintf(fp, "aaa\n");
  46. >      fprintf(fp, "%f\n", vect[i]);
  47. >    }
  48. >  }
  49. >
  50. >vect is a valid array, and the printf above prints the
  51. >correct value.  I said earlier that fp had a correct 
  52. >value.  But when the first fprintf is reached, I get
  53. >a segmentation fault...  using GDB, I did a "where" where
  54. >the seg. fault occured, and here is the result:
  55. >
  56. >(gdb) where
  57. >#0  0xfabc814 in __malloc () at malloc.c:303
  58. >#1  0xfabf1e4 in _malloc () at malloc.c:484
  59. >#2  0xfac6830 in _findbuf () at _findbuf.c:96
  60. >#3  0xfabca38 in _doprnt () at doprnt.c:1588
  61. >#4  0xfac7ffc in fprintf () at fprintf.c:45
  62. >#5  0x402c04 in writevector (fp=0xfb52914, len=128, vect=0x100022f0) at
  63. >mlutil.c:99
  64. >#6  0x40256c in main () at online.c:219
  65. >
  66. >Any idea of what that could be will be welcome...
  67. >(my project is due for next monday...)
  68. >
  69. >
  70. >Olivier
  71.  
  72.  
  73. I doubt that you have included your entire program above.  The gdb
  74. trace indicated that you really crashed in malloc() which means that
  75. the heap has been corrupted by your program.  There is a lot of 
  76. information in the FAQ about malloc() crashes, and how they are
  77. caused.  :-)
  78.  
  79.             Fletcher.Glenn@ov.com
  80.  
  81.  
  82.